package com.hrbxlh.support.web.attendance.controller;

import com.hrbxlh.pojo.SysUser;
import com.hrbxlh.support.web.attendance.po.RecordPO;
import com.hrbxlh.support.web.attendance.service.AttenceService;
import com.hrbxlh.support.web.attendance.vo.AttenceConfigVO;
import com.hrbxlh.util.ResultEnum;
import com.hrbxlh.virtualization.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

import static com.hrbxlh.support.web.auth.constant.ServiceConstant.USER_INFO;

/**
 * @author lx
 */
@RestController
@RequestMapping("/mobile/attendance")
@Slf4j
public class AttenceController {

    @Autowired
    private AttenceService attenceService;

    /**
     * 获取当前用户打卡情况
     */
    @GetMapping("/user")
    public Result<List<AttenceConfigVO>> listUserAttendance(HttpSession session) {
        SysUser user = (SysUser) session.getAttribute(USER_INFO);
        if (user == null) {
            return Result.error(ResultEnum.SESSION_TIMEOUT.getMessage());
        }
        List<AttenceConfigVO> configs = attenceService.listUserAttendance(user);
        if (CollectionUtils.isEmpty(configs)) {
            return Result.error(com.hrbxlh.virtualization.result.ResultEnum.NOT_FOUND);
        }
        return Result.success(configs);
    }

    /**
     * 打卡
     */
    @PostMapping("/record")
    public Result<String> record(@RequestBody @Valid RecordPO recordPO, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute(USER_INFO);
        if (user == null) {
            return Result.error(ResultEnum.SESSION_TIMEOUT.getMessage());
        }
        String recordTime = attenceService.record(recordPO, user);
        return Result.success(recordTime);
    }

}
